/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manejadorimagen;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JSlider;

/**
 *
 * @author ale
 */
public class vtnPrincipal extends javax.swing.JFrame {

    /**
     * Creates new form vtnPrincipal
     */
    private JFileChooser fc;
    private File imagen;
    private GestorImagen GestorImagen;
    
    
    
    public vtnPrincipal() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPestanias = new javax.swing.JTabbedPane();
        pnlInicio = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        lblDireccionArchivo = new javax.swing.JLabel();
        pnlRedimencionado = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lblDibujoRedimencionado = new javax.swing.JLabel();
        sliderRedimencionar = new javax.swing.JSlider();
        lblRedim = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Prueba Manipulacion de  Imagenes");
        setName("jframePrincipal"); // NOI18N

        jPestanias.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 51)));
        jPestanias.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPestaniasMouseClicked(evt);
            }
        });

        jButton1.setText("Cargar Imagen");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lblDireccionArchivo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout pnlInicioLayout = new javax.swing.GroupLayout(pnlInicio);
        pnlInicio.setLayout(pnlInicioLayout);
        pnlInicioLayout.setHorizontalGroup(
            pnlInicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInicioLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDireccionArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlInicioLayout.setVerticalGroup(
            pnlInicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInicioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInicioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDireccionArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(344, Short.MAX_VALUE))
        );

        lblDireccionArchivo.getAccessibleContext().setAccessibleName("jDireccionArchivo");

        jPestanias.addTab("Inicio", pnlInicio);

        jLabel1.setText("jLabel1");

        lblDibujoRedimencionado.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        sliderRedimencionar.setMinimum(10);
        sliderRedimencionar.setPaintLabels(true);
        sliderRedimencionar.setPaintTicks(true);
        sliderRedimencionar.setToolTipText("");
        sliderRedimencionar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        sliderRedimencionar.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderRedimencionarStateChanged(evt);
            }
        });

        javax.swing.GroupLayout pnlRedimencionadoLayout = new javax.swing.GroupLayout(pnlRedimencionado);
        pnlRedimencionado.setLayout(pnlRedimencionadoLayout);
        pnlRedimencionadoLayout.setHorizontalGroup(
            pnlRedimencionadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRedimencionadoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlRedimencionadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlRedimencionadoLayout.createSequentialGroup()
                        .addComponent(lblRedim, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sliderRedimencionar, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(193, 193, 193)
                .addComponent(lblDibujoRedimencionado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlRedimencionadoLayout.setVerticalGroup(
            pnlRedimencionadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRedimencionadoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlRedimencionadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDibujoRedimencionado, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
                    .addGroup(pnlRedimencionadoLayout.createSequentialGroup()
                        .addGroup(pnlRedimencionadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(sliderRedimencionar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblRedim, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPestanias.addTab("Redimencionado", pnlRedimencionado);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPestanias, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPestanias)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Se buscara la imagen a mostrar en la pantalla
        //Se inicializa el selector de archivos si ya no esta nicializado
        if(this.getFc()==null)
        {
            this.setFc(new javax.swing.JFileChooser());
        }
        int returnVal = this.getFc().showDialog(this.rootPane, null);
        //capturar la direccion del archivo seleccionado
        if (returnVal == this.getFc().APPROVE_OPTION) 
        {
            this.setImagen(this.getFc().getSelectedFile());
            //Se mostrara en un label la direccion absoluta del archivo seleccionado
            this.lblDireccionArchivo.setText(this.getImagen().getAbsolutePath());
            //Se pasara al Gestor de Imagen la direccion del archivo seleccionado
            if(this.getGestorImagen()==null)this.setGestorImagen(new GestorImagen());
            this.getGestorImagen().cargarImagen(this.getImagen().getAbsolutePath());
            panelDibujo panel = new panelDibujo(this.getGestorImagen().getImg());
            //getContentPane().add(panel);
            this.pnlInicio.add(panel);
            repaint();
            
            //se dibuja en el panel la imagen seleccionada
            //this.repaint();
        }
        //Se actualiza el nombre del archivo en el lbl
        //this.lblDireccionArchivo.setText(this.getImagen().getAbsolutePath());
        //liberar al Selector de Archivos del archivo seleccionado
        //this.getFc().setSelectedFile(null);
 
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jPestaniasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPestaniasMouseClicked
        if(this.getGestorImagen()!=null)
        {
            panelDibujo panel = new panelDibujo(this.getGestorImagen().getImg());
            //getContentPane().add(panel);
            this.pnlRedimencionado.add(panel);
            repaint();
        }
    }//GEN-LAST:event_jPestaniasMouseClicked

    private void sliderRedimencionarStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderRedimencionarStateChanged
        JSlider source = (JSlider)evt.getSource();
        int valorRedimencionar;
        if(!source.getValueIsAdjusting())
        {
            valorRedimencionar = (int)source.getValue();
            this.lblRedim.setText(Integer.toString(valorRedimencionar)+" %");
        }
    }//GEN-LAST:event_sliderRedimencionarStateChanged
    
    /*public void paint(Graphics g) 
    {
        //Llamada a la instruccion super.paint(g) para mantener la configuracion base
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        Graphics2D g3 = (Graphics2D)g;
        Graphics2D g4;
        BufferedImage orig;
        try {
            orig = ImageIO.read(new File("D:\\006.jpg"));
            
            //imagen original---------------------------------------------------
            g.drawImage(orig,0,40,this);
            
            //imagen en escala de grises----------------------------------------
            BufferedImage imgGris = new BufferedImage   (
                                                        orig.getWidth(),
                                                        orig.getHeight(),
                                                        BufferedImage.TYPE_BYTE_GRAY
                                                        );
            g4 = imgGris.createGraphics();
            g4.drawImage(orig,0,0,null);
            g.drawImage(imgGris,orig.getWidth(),40,this);
            
            //imagen reducida---------------------------------------------------
            BufferedImage imgRed = new BufferedImage    (
                                                        orig.getWidth()/2,
                                                        orig.getHeight()/2,
                                                        BufferedImage.TYPE_INT_BGR
                                                        );
            g4 = imgRed.createGraphics();
            g4.drawImage(orig, 0, 0, orig.getWidth()/2,orig.getHeight()/2,null);
            g.drawImage(imgRed,orig.getWidth()*2,40,this);
            
            //mensajes de la cabecera-------------------------------------------
            g.drawString( "Original",100,40 );
            g.drawString( "Gris",300,40 );
            g.drawString( "Reducido",500,40 );
            
        } catch (IOException ex) {
            Logger.getLogger(vtnPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }*/
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(vtnPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(vtnPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(vtnPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(vtnPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new vtnPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTabbedPane jPestanias;
    private javax.swing.JLabel lblDibujoRedimencionado;
    private javax.swing.JLabel lblDireccionArchivo;
    private javax.swing.JLabel lblRedim;
    private javax.swing.JPanel pnlInicio;
    private javax.swing.JPanel pnlRedimencionado;
    private javax.swing.JSlider sliderRedimencionar;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the fc
     */
    public JFileChooser getFc() {
        return fc;
    }

    /**
     * @param fc the fc to set
     */
    public void setFc(JFileChooser fc) {
        this.fc = fc;
    }

    /**
     * @return the imagen
     */
    public File getImagen() {
        return imagen;
    }

    /**
     * @param imagen the imagen to set
     */
    public void setImagen(File imagen) {
        this.imagen = imagen;
    }

    /**
     * @return the GestorImagen
     */
    public GestorImagen getGestorImagen() {
        return GestorImagen;
    }

    /**
     * @param GestorImagen the GestorImagen to set
     */
    public void setGestorImagen(GestorImagen GestorImagen) {
        this.GestorImagen = GestorImagen;
    }
}
